import  { Orderinfo } from './entity'
import ordAndOrdDetailDao from './OrdAndOrdDetailDao'
import ShopCartDao from '../shopcart/dao/ShopCartDao';
import { addEntryToArr } from '@/modules/commontypes'
class OrdAndOrdDetailService {
  static OrdAndOrdDetailService:OrdAndOrdDetailService = new OrdAndOrdDetailService();
  async submitOrder(ordAndOrdDetail: Orderinfo) {
    // 1.保存订单
    const orderinfo: Orderinfo = {
      ordertime: ordAndOrdDetail.ordertime,
      customerid: ordAndOrdDetail.customerid,
      orderstatus:1
    }
    const dbOrderid:number = (await ordAndOrdDetailDao.addOrderinfo(orderinfo))[0];
    // 2.拼接订单id和订单详情
    const orderDetailLst=ordAndOrdDetail.orderDetailList!;
    const lastOrderDetailLst=addEntryToArr(orderDetailLst,"orderid",dbOrderid);// 塞进去一个订单id
    // 3.保存订单详情
    let dbOrderDetailId:number;
    for (let orderDetail of lastOrderDetailLst){
      dbOrderDetailId=(await ordAndOrdDetailDao.addOrderDetail(orderDetail))[0];
      orderDetail.orderdetailid=dbOrderDetailId;
      // 4.删除对应购物车列表图书记录
      await ShopCartDao.deOneBookFrmSc(orderDetail.shopcartid!)
      }
      ordAndOrdDetail.orderDetailList!=lastOrderDetailLst
    return ordAndOrdDetail;
  }
  async findCurUsrOrdAndOrdDetail(customerid: string){
    return (await ordAndOrdDetailDao.findCurUsrordAndordDetail(customerid))[0];
  }
  async uptOrdStatusByOrdId(orderid:number,orderstatus:number):Promise<number>{
    // 返回影响条数   所有语言的后端都是一样的
    const uptRows:number=(await ordAndOrdDetailDao.uptOrdStatusByOrdId(orderid,orderstatus))[0];
    return uptRows;
  }

}

export default OrdAndOrdDetailService.OrdAndOrdDetailService;
